<?php

namespace app\controllers; //命名空间

use yii\web\Controller; // 导入Controller
use Yii; // 导入Yii
use app\common\Util;  // 导入公共方法


// AppletController 控制器类
class OpenidController extends Controller{
        public $enableCsrfValidation = false;
	// 想微信发起请求获取openId 和 session_key
	public function actionGetopenid(){
		if(Yii::$app->request->isPost){
			// 获取数据
			$postData =  Yii::$app->request->post();
			
			$grant_type = 'authorization_code';
			$APPID = Yii::$app->params['WECHAT']['APPID']; 
			$APPSECRET = Yii::$app->params['WECHAT']['APPSECRET'];

			$url_openId = Yii::$app->params['URL']['OPENID'].'?appid='.$APPID.'&secret='.$APPSECRET.'&js_code='.$postData['js_code'].'&grant_type='.$grant_type; 
			$html_openId = Util::getSSL($url_openId);
			$html_openId = json_decode($html_openId, true);
               //       var_dump($html_openId);		
			$data = array(
				'openId' => $html_openId['openid'],
				'session_key' => $html_openId['session_key']
			);
			
			echo json_encode( array('error'=>0,'data'=>$data) );
		}else{
			echo json_encode( array('error'=>1,'data'=>'提交数据方式有误') );   
		}   
	}
}
